using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WpfApp1.Models;
using WpfApp1.Services;

namespace WpfApp1.Views
{
    /// <summary>
    /// DataHistoryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataHistoryWindow : Window
    {
        private readonly DatabaseService _databaseService;
        private List<TcpDataRecord> _allRecords;

        public DataHistoryWindow()
        {
            InitializeComponent();
            _databaseService = new DatabaseService();
            _allRecords = new List<TcpDataRecord>();
            
            // 绑定选择事件
            dgRecords.SelectionChanged += DgRecords_SelectionChanged;
        }

        /// <summary>
        /// 窗口加载事件
        /// </summary>
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            await LoadRecordsAsync();
        }

        /// <summary>
        /// 刷新按钮点击事件
        /// </summary>
        private async void BtnRefresh_Click(object sender, RoutedEventArgs e)
        {
            await LoadRecordsAsync();
        }

        /// <summary>
        /// 清空记录按钮点击事件
        /// </summary>
        private async void BtnClear_Click(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("确定要清空所有记录吗？此操作不可恢复！", 
                                       "确认清空", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            
            if (result == MessageBoxResult.Yes)
            {
                try
                {
                    await _databaseService.ClearAllRecordsAsync();
                    await LoadRecordsAsync();
                    MessageBox.Show("所有记录已清空", "操作完成", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"清空记录失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        /// <summary>
        /// 数据网格选择变化事件
        /// </summary>
        private void DgRecords_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (dgRecords.SelectedItem is TcpDataRecord selectedRecord)
            {
                txtRawResponse.Text = selectedRecord.RawResponse;
                txtExtractedData.Text = selectedRecord.ExtractedData;
            }
            else
            {
                txtRawResponse.Text = string.Empty;
                txtExtractedData.Text = string.Empty;
            }
        }

        /// <summary>
        /// 加载记录
        /// </summary>
        private async Task LoadRecordsAsync()
        {
            try
            {
                _allRecords = await _databaseService.GetAllTcpDataRecordsAsync();
                dgRecords.ItemsSource = _allRecords;
                txtRecordCount.Text = $"记录数: {_allRecords.Count}";
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载记录失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
